/**
 * Created by Administrator on 2017/10/9.
 */
angular.module('module.set')
    .controller('MainController', MainController);
MainController.$inject = ['SetService', 'TableNumService', '$state', '$localStorage'];
function MainController(SetService, TableNumService, $state, $localStorage) {

    var vm = this;
    if (!$localStorage[environmental + "shopid"]) {
        $localStorage.$reset();
        $state.go('login');
    }
    var shopid = $localStorage[environmental + "shopid"];

    vm.exitlogin = exitlogin;
    vm.setLowPower = setLowPower;
    vm.goAuto = goAuto;
    vm.sureBtn = sureBtn; // 模态框
    vm.cancelBtn = cancelBtn; // 模态框

    SetService.getShopinfo($localStorage[environmental + "shopid"], function (data) {
        vm.info = data;
    });

    function setLowPower() {
        $('#lowPower').modal('show');
        vm.sure = function sure() {
            $("#miansurebtn").attr("disabled", true);
            $('#lowPower').modal('hide');
            if (!vm.power) {
                vm.messageVisible = true;
                vm.message = "请输入低电量提醒！";
                $("#miansurebtn").attr("disabled", false);
                return;
            }
            vm.info.chargeTisement = vm.power;
            var shopinfo = vm.info;
            SetService.setLowPower(shopinfo, function (data) {
                $("#miansurebtn").attr("disabled", false);
            }, function (error) {
                $("#miansurebtn").attr("disabled", false);
            })
        }
    }

    function exitlogin() {
        $localStorage.$reset();
        $state.go('login')
    }

    function goAuto() {
        TableNumService.getTableNumByShopId($localStorage[environmental + "shopid"], function (data) {
            if (data.length == 0) {
                $('#table').modal('show');
                setTimeout(function () {
                    $('#table').modal('hide')
                }, 5000);
            }
            else {
                $state.go('autoStart');
            }
        })
    }

    function sureBtn() {
        vm.messageVisible = false;
    }

    function cancelBtn() {
        vm.messageVisible = false;
    }
}